<% content_for :page_title do %>
  <%= plural_resource_name(Spree::Order) %>
<% end %>

<% content_for :page_actions do %>
  <%= button_link_to Spree.t(:new_order), new_admin_order_url, :class => "btn-success", :icon => 'add', :id => 'admin_new_order' %>
<% end if can? :create, Spree::Order %>

<% content_for :table_filter do %>
  <div data-hook="admin_orders_index_search">

    <%= search_form_for [:admin, @search] do |f| %>
      <div class="row">
        <div class="date-range-filter col-md-8">
          <div class="form-group">
            <%= label_tag :q_created_at_gt, Spree.t(:date_range) %>
            <div class="row no-padding-bottom">
              <div class="col-md-6">
                <div class="input-group">
                  <%= f.text_field :created_at_gt, :class => 'datepicker datepicker-from form-control', :value => params[:q][:created_at_gt], :placeholder => Spree.t(:start) %>
                  <span class="input-group-addon">
                    <i class="icon icon-calendar"></i>
                  </span>
                </div>

              </div>
              <div class="col-md-6">
                <div class="input-group">
                  <%= f.text_field :created_at_lt, :class => 'datepicker datepicker-to form-control', :value => params[:q][:created_at_lt], :placeholder => Spree.t(:stop) %>
                  <span class="input-group-addon">
                    <i class="icon icon-calendar"></i>
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="col-md-4">
          <div class="form-group">
            <%= label_tag :q_number_cont, Spree.t(:order_number, :number => '') %>
            <%= f.text_field :number_cont, class: 'form-control js-quick-search-target' %>
          </div>
        </div>

      </div>

      <div class="row">

        <div class="col-md-4">
          <div class="form-group">
            <%= label_tag :q_state_eq, Spree.t(:status) %>
            <%= f.select :state_eq, Spree::Order.state_machines[:state].states.map {|s| [Spree.t("order_state.#{s.name}"), s.value]}, {:include_blank => true}, :class => 'select2 js-filterable' %>
          </div>
        </div>

        <div class="col-md-4">
          <div class="form-group">
            <%= label_tag :q_payment_state_eq, Spree.t(:payment_state) %>
            <%= f.select :payment_state_eq, Spree::Order::PAYMENT_STATES.map {|s| [Spree.t("payment_states.#{s}"), s]}, {:include_blank => true}, :class => 'select2 js-filterable' %>
          </div>
        </div>

        <div class="col-md-4">
          <div class="form-group">
            <%= label_tag :q_shipment_state_eq, Spree.t(:shipment_state) %>
            <%= f.select :shipment_state_eq, Spree::Order::SHIPMENT_STATES.map {|s| [Spree.t("shipment_states.#{s}"), s]}, {:include_blank => true}, :class => 'select2 js-filterable' %>
          </div>
        </div>

      </div>

      <div class="row">

        <div class="col-md-4">
          <div class="form-group">
            <%= label_tag :q_bill_address_firstname_start, Spree.t(:first_name_begins_with) %>
            <%= f.text_field :bill_address_firstname_start, class: 'form-control' %>
          </div>
        </div>

        <div class="col-md-4">
          <div class="form-group">
            <%= label_tag :q_bill_address_lastname_start, Spree.t(:last_name_begins_with) %>
            <%= f.text_field :bill_address_lastname_start, class: 'form-control' %>
          </div>
        </div>

        <div class="col-md-4">
          <div class="form-group">
            <%= label_tag :q_email_cont, Spree.t(:email) %>
            <%= f.text_field :email_cont, class: 'form-control js-filterable' %>
          </div>
        </div>

      </div>

      <div class="row">

        <div class="col-md-4">
          <div class="form-group">
            <%= label_tag :q_line_items_variant_sku_eq, Spree.t(:sku) %>
            <%= f.text_field :line_items_variant_sku_eq, class: 'form-control' %>
          </div>
        </div>

        <div class="col-md-4">
          <div class="form-group">
            <%= label_tag :q_promotions_id_in, Spree.t(:promotion) %>
            <%= f.select :promotions_id_in, Spree::Promotion.applied.pluck(:name, :id), {:include_blank => true}, :class => 'select2' %>
          </div>
        </div>

        <div class="col-md-4">

          <div class="form-group">

            <div class="checkbox">
              <%= label_tag 'q_completed_at_not_null' do %>
                <%= f.check_box :completed_at_not_null, {:checked => @show_only_completed}, '1', '0' %>
                <%= Spree.t(:show_only_complete_orders) %>
              <% end %>
            </div>

            <div class="checkbox">
              <%= label_tag 'q_considered_risky_eq' do %>
                <%= f.check_box :considered_risky_eq, {:checked => (params[:q][:considered_risky_eq] == '1')}, '1', '' %>
                <%= Spree.t(:show_only_considered_risky) %>
              <% end %>
            </div>

          </div>

        </div>

      </div>

      <div data-hook="admin_orders_index_search_buttons" class="form-actions">
        <%= button Spree.t(:filter_results), 'search' %>
      </div>

    <% end %>

  </div>

<% end %>

<%= render 'spree/admin/shared/index_table_options', collection: @orders %>

<% if @orders.any? %>
  <table class="table" id="listing_orders" data-hook>
    <thead>
      <tr data-hook="admin_orders_index_headers">
        <% if @show_only_completed %>
          <th><%= sort_link @search, :completed_at,   I18n.t(:completed_at, :scope => 'activerecord.attributes.spree/order') %></th>
        <% else %>
          <th><%= sort_link @search, :created_at,     I18n.t(:created_at, :scope => 'activerecord.attributes.spree/order') %></th>
        <% end %>
        <th><%= sort_link @search, :number,           I18n.t(:number, :scope => 'activerecord.attributes.spree/order') %></th>
        <th><%= sort_link @search, :considered_risky, I18n.t(:considered_risky, :scope => 'activerecord.attributes.spree/order') %></th>
        <th><%= sort_link @search, :state,            I18n.t(:state, :scope => 'activerecord.attributes.spree/order') %></th>
        <th><%= sort_link @search, :payment_state,    I18n.t(:payment_state, :scope => 'activerecord.attributes.spree/order') %></th>
         <% if Spree::Order.checkout_step_names.include?(:delivery) %>
          <th><%= sort_link @search, :shipment_state, I18n.t(:shipment_state, :scope => 'activerecord.attributes.spree/order') %></th>
         <% end %>
        <th><%= sort_link @search, :email,            I18n.t(:email, :scope => 'activerecord.attributes.spree/order') %></th>
        <th><%= sort_link @search, :total,            I18n.t(:total, :scope => 'activerecord.attributes.spree/order') %></th>
        <th data-hook="admin_orders_index_header_actions" class="actions"></th>
      </tr>
    </thead>
    <tbody>
    <% @orders.each do |order| %>
      <tr data-hook="admin_orders_index_rows" class="state-<%= order.state.downcase %> <%= cycle('odd', 'even') %>">
        <td>
          <%= order_time(@show_only_completed ? order.completed_at : order.created_at) %>
        </td>
        <td><%= link_to order.number, edit_admin_order_path(order) %></td>
        <td>
          <span class="label label-<%= order.considered_risky ? 'considered_risky' : 'considered_safe' %>">
            <%= order.considered_risky ? Spree.t("risky") : Spree.t("safe") %>
          </span>
        </td>
        <td>
          <span class="label label-<%= order.state.downcase %>"><%= Spree.t("order_state.#{order.state.downcase}") %></span>
          <span class="icon icon-filter filterable js-add-filter" data-ransack-field="q_state_eq" data-ransack-value="<%= order.state %>"></span>
        </td>
        <td>
          <% if order.payment_state %>
            <span class="label label-<%= order.payment_state %>"><%= link_to Spree.t("payment_states.#{order.payment_state}"), admin_order_payments_path(order) %></span>
            <span class="icon icon-filter filterable js-add-filter" data-ransack-field="q_payment_state_eq" data-ransack-value="<%= order.payment_state %>"></span>
          <% end %>
        </td>
        <% if Spree::Order.checkout_step_names.include?(:delivery) %>
          <td>
            <% if order.shipment_state %>
              <span class="label label-<%= order.shipment_state %>"><%= Spree.t("shipment_states.#{order.shipment_state}") %></span>
              <span class="icon icon-filter filterable js-add-filter" data-ransack-field="q_shipment_state_eq" data-ransack-value="<%= order.shipment_state %>"></span>
            <% end %>
          </td>
        <% end %>
        <td>
          <% if order.user %>
            <%= link_to order.email, edit_admin_user_path(order.user) %>
          <% else %>
            <%= mail_to order.email %>
          <% end %>
          <% if order.user || order.email %>
            <span class="icon icon-filter filterable js-add-filter" data-ransack-field="q_email_cont" data-ransack-value="<%= order.email %>"></span>
          <% end %>
        </td>
        <td><%= order.display_total.to_html %></td>
        <td class='actions actions-1' data-hook="admin_orders_index_row_actions">
          <%= link_to_edit_url edit_admin_order_path(order), :title => "admin_edit_#{dom_id(order)}", :no_text => true %>
        </td>
      </tr>
    <% end %>
    </tbody>
  </table>
<% else %>
  <div class="alert alert-info no-objects-found">
    <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Order)) %>,
    <%= link_to(Spree.t(:add_one), new_admin_order_url) if can? :create, Spree::Order %>!
  </div>
<% end %>

<%= render 'spree/admin/shared/index_table_options', collection: @orders, simple: true %>
